package com.edu.etu.gui.checkit;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.HashMap;

public class ViewListActivity extends ActionBarActivity implements View.OnClickListener {

    SQLiteHelperList openHelperList;
    SQLiteDatabase db;
    ContentValues cv;
    Integer idElement;
    LinearLayout main;

    TextView nameList;
    int idThisList=0;
    private HashMap<View,Integer> mapViewElement = new HashMap<View,Integer>();
    int wrapContent = LinearLayout.LayoutParams.WRAP_CONTENT;
    Cursor cursor;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_list);

        openHelperList = new SQLiteHelperList(getApplicationContext());
        db = openHelperList.getWritableDatabase();
        cv = new ContentValues();

        LinearLayout.LayoutParams lParams = new LinearLayout.LayoutParams(wrapContent, wrapContent);
        lParams.gravity = Gravity.LEFT;

        ImageButton btnDelAllList = (ImageButton) findViewById(R.id.btnDelAllList);
        btnDelAllList.setOnClickListener(this);

        ImageButton openEditActivity = (ImageButton)findViewById(R.id.openEditActivity);
        openEditActivity.setOnClickListener(this);

        main=(LinearLayout)findViewById(R.id.mainListElement);
        nameList = (TextView)findViewById(R.id.list_name);

        cursor = db.query("Lists", null, null, null, null, null, null);
        if(cursor.moveToFirst())
        {
            do {
                if(cursor.getInt(cursor.getColumnIndex("_id"))==getIntent().getIntExtra("ListId",-1))
                {
                    idThisList=cursor.getInt(cursor.getColumnIndex("_id"));
                    nameList.setText(cursor.getString(cursor.getColumnIndex("nameLists")));
                }
            }
            while (cursor.moveToNext());
        }
        cursor.close();

        draw_list();

    }

    public void draw_list()
    {
        mapViewElement.clear();
        main.removeAllViews();
        cursor = db.query("Elements", null, null, null, null, null, null);
        if (cursor.moveToFirst()) {
            int idList = cursor.getColumnIndex("idList");
            int nameElements = cursor.getColumnIndex("nameElements");
            int boolElements = cursor.getColumnIndex("boolElements");
            int idElement = cursor.getColumnIndex("_id2");
            do {
                if(getIntent().getIntExtra("ListId",-1)==cursor.getInt(idList))
                {
                    LayoutInflater inflater = getLayoutInflater();
                    View list_element = inflater.inflate(R.layout.view_element,null,false);
                    CheckBox element_checkbox =  (CheckBox)list_element.findViewById(R.id.viewCheckBoxElement);
                    element_checkbox.setText(cursor.getString(nameElements));
                    element_checkbox.setChecked(cursor.getInt(boolElements) == 1);
                    main.addView(list_element);
                    mapViewElement.put(element_checkbox, cursor.getInt(idElement));
                    element_checkbox.setOnClickListener(this);
                }
            }
            while (cursor.moveToNext());
        }
        cursor.close();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        
        // Inflate the menu; this adds items to the action bar if it is present.
        //getMenuInflater().inflate(R.menu.list_user, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View v) {
       switch (v.getId())
            {
                case R.id.viewCheckBoxElement:
                    idElement=mapViewElement.get(v);
                    if(idElement!=null)
                    {
                        CheckBox cb = (CheckBox)v;
                        cursor = db.query("Elements", null, null, null, null, null, null);
                        if (cursor.moveToFirst()) {
                            do {
                                if(idElement==cursor.getInt(cursor.getColumnIndex("_id2")))
                                {
                                    String strFilter = "_id2="+idElement;
                                    ContentValues args = new ContentValues();
                                    args.put(openHelperList.boolElements,cb.isChecked());
                                    db.update(openHelperList.NameTableElements,args,strFilter,null);
                                }
                            }
                            while (cursor.moveToNext());
                        }
                        cursor.close();
                    }
                    break;
                case R.id.openEditActivity:
                    Intent intent = new Intent(this, EdiListActivity.class);
                    intent.putExtra("ListId",getIntent().getIntExtra("ListId",-1));
                    startActivity(intent);
                    break;
                case R.id.btnDelAllList:
                    Log.d("myLog",""+idThisList);

                    cursor = db.query("Lists", null, null, null, null, null, null);
                    if (cursor.moveToFirst()) {
                        do {
                            if(cursor.getInt(cursor.getColumnIndex("_id"))==idThisList)
                            {
                                String i = cursor.getString(cursor.getColumnIndex("_id"));
                                if (i.equalsIgnoreCase("")) {
                                    break;
                                }
                                db.execSQL("delete from Lists where _id='"+i+"'");
                            }
                        }
                        while (cursor.moveToNext());
                    }
                    cursor.close();

                    cursor = db.query("Elements", null, null, null, null, null, null);
                    if (cursor.moveToFirst()) {
                        do {
                            if(idThisList==cursor.getInt(cursor.getColumnIndex("idList")))
                            {
                                String i = cursor.getString(cursor.getColumnIndex("_id2"));
                                if (i.equalsIgnoreCase("")) {
                                    break;
                                }
                                db.execSQL("delete from Elements where _id2='"+i+"'");
                            }
                        }
                        while (cursor.moveToNext());
                    }
                    cursor.close();
                    Intent finish = new Intent(this, MainActivity.class);
                    startActivity(finish);
                    finish();
                    break;
                }
            }

    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        draw_list();
        Log.d("myLog", "onRestoreInstanceState View");
    }
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        draw_list();
        Log.d("myLog", "onSaveInstanceState View");
    }


}
